package org.behrang.macbeans.tab.camino;

import org.behrang.macbeans.tab.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Polygon;
import java.awt.Rectangle;
import javax.swing.JComponent;
import org.netbeans.swing.tabcontrol.plaf.TabPainter;

/**
 *
 * @author behrangsa
 */
public class CaminoEditorTabCellRenderer extends MacBeansTabCellRenderer {

    public CaminoEditorTabCellRenderer() {
        super(CaminoEditorTabPainter.INSTANCE, CaminoEditorTabPainter.INSTANCE, CaminoEditorTabPainter.INSTANCE, new Dimension(14, 0));
    }
    
    private static final class CaminoEditorTabPainter implements TabPainter, CaminoColors {

        private static final CaminoEditorTabPainter INSTANCE = new CaminoEditorTabPainter();
        private static final Insets INSETS = new Insets(0, 0, 0, 0);
        
        public void getCloseButtonRectangle(JComponent renderer, Rectangle rect, Rectangle bounds) {
            // rect.x = rect.y = rect.width = rect.height = 0;            
        }

        public Polygon getInteriorPolygon(Component renderer) {
            return new Polygon(
                    new int[]{0, renderer.getWidth(), renderer.getWidth(), 0},
                    new int[]{0, 0, renderer.getHeight(), renderer.getHeight()}, 4);
        }

        protected void paintSelected(Graphics g, Component renderer) {            
            final Rectangle bnd = renderer.getBounds();
            final int x = 0, y = 0, w = bnd.width, h = bnd.height;
            
            // upper line
            g.setColor(UL);
            g.drawLine(x, y, w, y);
            
            // background
            g.setColor(SB1);
            g.fillRect(x + 1, y + 1, w - 2, 10);
            g.setColor(SB2);
            g.fillRect(x + 1, y + 11, w - 2, 7);
            
            // left lines
            int x0 = w - 2, x1 = x0;
            int y0 = 1;
            g.setColor(SS1);
            g.drawLine(x0, y0, x0, y0++);
            g.setColor(SS2);
            g.drawLine(x0, y0, x0, y0++);
            g.setColor(SS3);
            g.drawLine(x0, y0, x0, y0++);
            g.setColor(SS4);
            g.drawLine(x0, y0, x0, y0++);
            g.setColor(SS5);
            g.drawLine(x0, y0, x0, y0++);
            g.setColor(SS6);
            g.drawLine(x0, y0, x0, y0 + 5); y0 += 5;
            g.setColor(SS7);
            g.drawLine(x0, y0, x0, y0 + 7); y0 += 7;
            g.setColor(SS8);
            g.drawLine(x0, y0, x0, y0++);
            g.setColor(SS9);
            g.drawLine(x0, y0, x0, y0);
            
            // left lines
            x0 = w - 1; x1 = x0;
            y0 = 1;
            g.setColor(SS10);
            g.drawLine(x0, y0, x0, y0++);
            g.setColor(SS11);
            g.drawLine(x0, y0, x0, y0++);
            g.setColor(SS12);
            g.drawLine(x0, y0, x0, y0++);
            g.setColor(SS13);
            g.drawLine(x0, y0, x0, y0++);
            g.setColor(SS14);
            g.drawLine(x0, y0, x0, y0++);
            g.setColor(SS15);
            g.drawLine(x0, y0, x0, y0 + 5); y0 += 5;
            g.setColor(SS16);
            g.drawLine(x0, y0, x0, y0 + 7); y0 += 6;
            g.setColor(SS17);            
            g.drawLine(x0, y0, x0, y0++);
            g.setColor(SS18);
            g.drawLine(x0, y0, x0, y0);
            
            // right lines
            x0 = 1; x1 = x0;
            y0 = 1;
            g.setColor(SS1);
            g.drawLine(x0, y0, x0, y0++);
            g.setColor(SS2);
            g.drawLine(x0, y0, x0, y0++);
            g.setColor(SS3);
            g.drawLine(x0, y0, x0, y0++);
            g.setColor(SS4);
            g.drawLine(x0, y0, x0, y0++);
            g.setColor(SS5);
            g.drawLine(x0, y0, x0, y0++);
            g.setColor(SS6);
            g.drawLine(x0, y0, x0, y0 + 5); y0 += 5;
            g.setColor(SS7);
            g.drawLine(x0, y0, x0, y0 + 7); y0 += 7;
            g.setColor(SS8);
            g.drawLine(x0, y0, x0, y0++);
            g.setColor(SS9);
            g.drawLine(x0, y0, x0, y0);
            
            // right lines lines
            x0 = 0; x1 = x0;
            y0 = 1;
            g.setColor(SS10);
            g.drawLine(x0, y0, x0, y0++);
            g.setColor(SS11);
            g.drawLine(x0, y0, x0, y0++);
            g.setColor(SS12);
            g.drawLine(x0, y0, x0, y0++);
            g.setColor(SS13);
            g.drawLine(x0, y0, x0, y0++);
            g.setColor(SS14);
            g.drawLine(x0, y0, x0, y0++);
            g.setColor(SS15);
            g.drawLine(x0, y0, x0, y0 + 5); y0 += 5;
            g.setColor(SS16);
            g.drawLine(x0, y0, x0, y0 + 7); y0 += 7;
            g.setColor(SS17);
            g.drawLine(x0, y0, x0, y0++);
            g.setColor(SS18);
            g.drawLine(x0, y0, x0, y0);
            
            // bottom lines
            g.setColor(SL1);
            g.drawLine(2, h - 6, w - 3, h - 6);
            g.setColor(SL2);
            g.drawLine(2, h - 5, w - 3, h - 5);
            g.setColor(B3);
            g.drawLine(0, h - 4, w, h - 4);
            g.setColor(B4);
            g.drawLine(0, h - 3, w, h - 3);
            g.setColor(B5);            
            g.drawLine(0, h - 2, w, h - 2);
        }
        
        public void paintInterior(Graphics g, Component renderer) {
            final Color oldColor = g.getColor();
            final Rectangle bnd = renderer.getBounds();
            final int x = 0, y = 0, w = bnd.width, h = bnd.height;
            
            if (((CaminoEditorTabCellRenderer) renderer).isSelected()) {
                paintSelected(g, renderer);
                return;
            }
            
            // background
            g.setColor(BG);
            g.fillRect(x, y + 1, w, h - 6);
            
            // upper line
            g.setColor(UL);
            g.drawLine(x, y, w, y);
            
            if (!((CaminoEditorTabCellRenderer) renderer).isPreviousTabSelected()) {
                // left lines
                g.setColor(L1);
                g.drawLine(0, 1, 0, h - 6);
                g.setColor(L2);
                g.drawLine(1, 1, 1, 1);
                g.setColor(L3);
                g.drawLine(1, 2, 1, h - 7);
            }          
            
            // bottom lines
            g.setColor(B1);
            g.drawLine(1, h - 6, w, h - 6);
            g.setColor(B2);
            g.drawLine(0, h - 5, w, h - 5);
            g.setColor(B3);
            g.drawLine(0, h - 4, w, h - 4);
            g.setColor(B4);
            g.drawLine(0, h - 3, w, h - 3);
            g.setColor(B5);            
            g.drawLine(0, h - 2, w, h - 2);
            
            // right lines
            if (((CaminoEditorTabCellRenderer) renderer).isRightmost()) {                
                g.setColor(L1);
                g.drawLine(w - 3, 1,  w - 3, h - 6);
                g.setColor(L2);
                g.drawLine(w - 2, 1, w - 2, 1);
                g.setColor(L3);
                g.drawLine(w - 2, 2, w - 2, h - 7);
            }
            
            g.setColor(oldColor);
        }

        public boolean supportsCloseButton(JComponent renderer) {
            return false;
            // return true;
        }

        public Insets getBorderInsets(Component c) {
            return INSETS;
        }

        public boolean isBorderOpaque() {
            return true;
        }

        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
            
        }        
    }
    
}
